home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / WINPROGS.ARJ / CHECKER1.C < prev    next >
Text File  |  1990-11-28  |  4KB  |  139 lines

  1. /*   Checker1.C   -  Mouse Hit Program
  2.                      Petzold
  3. */
  4.  
  5. #include <windows.h>
  6.  
  7. #define DIVISIONS 5
  8.  
  9. long FAR PASCAL WndProc (HWND, WORD, WORD, LONG);
  10.  
  11. int PASCAL WinMain (HANDLE hInstance,
  12.                     HANDLE hPrevInstance,
  13.                     LPSTR  lpszCmdParam,
  14.                     int    nCmdShow)
  15.                     
  16.   {
  17.   static char szAppName[] = "Checker1";
  18.   HWND        hwnd;
  19.   MSG         msg;
  20.   WNDCLASS    wndclass;
  21.   
  22.   if (!hPrevInstance)
  23.      {
  24.      wndclass.style            = CS_HREDRAW | CS_VREDRAW;
  25.      wndclass.lpfnWndProc      = WndProc;
  26.      wndclass.cbClsExtra       = 0;
  27.      wndclass.cbWndExtra       = 0;
  28.      wndclass.hInstance        = hInstance;
  29.      wndclass.hIcon            = LoadIcon (NULL, IDI_APPLICATION);
  30.      wndclass.hCursor          = LoadCursor (NULL, IDC_ARROW);
  31.      wndclass.hbrBackground    = GetStockObject (WHITE_BRUSH);
  32.      wndclass.lpszMenuName     = NULL;
  33.      wndclass.lpszClassName    = szAppName;
  34.      
  35.      RegisterClass(&wndclass);
  36.      }
  37.      
  38.   hwnd = CreateWindow (szAppName,
  39.                        "Checker1 Mouse Hit-Test Demo",
  40.                        WS_OVERLAPPEDWINDOW, 
  41.                        CW_USEDEFAULT,   
  42.                        CW_USEDEFAULT,
  43.                        CW_USEDEFAULT,   
  44.                        CW_USEDEFAULT,   
  45.                        NULL,
  46.                        NULL,
  47.                        hInstance,
  48.                        NULL);
  49.                        
  50.   ShowWindow (hwnd, nCmdShow);
  51.   UpdateWindow (hwnd);
  52.   
  53.   while (GetMessage (&msg, NULL, 0, 0))
  54.     {
  55.     TranslateMessage (&msg);
  56.     DispatchMessage  (&msg);
  57.     }
  58.     
  59.   return msg.wParam;
  60.   }
  61.   
  62. long FAR PASCAL WndProc (HWND hwnd,
  63.                          WORD message,
  64.                          WORD wParam,
  65.                          LONG lParam)
  66.                          
  67.   {
  68.   static BOOL fState[DIVISIONS][DIVISIONS];
  69.   static short cxBlock, cyBlock;
  70.   HDC          hdc;
  71.   PAINTSTRUCT  ps;
  72.   RECT         rect;
  73.   short        x, y;
  74.   char   szBuffer[80];
  75.     
  76.   switch (message)
  77.     {
  78.     case WM_SIZE:
  79.       cxBlock = LOWORD (lParam) / DIVISIONS;
  80.       cyBlock = HIWORD (lParam) / DIVISIONS;
  81.       return 0;
  82.       
  83.     case WM_LBUTTONDOWN:
  84.       x = LOWORD (lParam) / cxBlock;
  85.       y = HIWORD (lParam) / cyBlock;
  86.    
  87.  
  88. /*      wsprintf(szBuffer, "cxBlock = %d  cyBlock = %d",cxBlock,cyBlock);
  89.       MessageBox (hwnd, szBuffer, "Block Size", MB_ICONINFORMATION | MB_OK);
  90.       
  91.       wsprintf(szBuffer, "X = %d  Y = %d",x,y);
  92.       
  93.       MessageBox (hwnd, szBuffer, "X Y  -  Size", MB_ICONINFORMATION | MB_OK);
  94.       
  95. */
  96.       
  97.       if (x < DIVISIONS && y < DIVISIONS)
  98.         {
  99.         fState[x][y] ^= 1;
  100.         rect.left    = x * cxBlock;
  101.         rect.top     = y * cyBlock;
  102.         rect.right   = (x + 1) * cxBlock;
  103.         rect.bottom  = (y + 1) * cyBlock;
  104.         
  105.         InvalidateRect (hwnd, &rect, FALSE);
  106.         }
  107.       else
  108.         MessageBeep (0);
  109.       
  110.       return 0;
  111.       
  112.     case WM_PAINT:
  113.       hdc = BeginPaint (hwnd, &ps);
  114.       
  115.       for (x = 0; x < DIVISIONS; x++)
  116.         for (y = 0; y < DIVISIONS; y++)
  117.           {
  118.           Rectangle (hdc, x * cxBlock, y * cyBlock,
  119.                     (x + 1) * cxBlock, (y + 1) * cyBlock);
  120.           if (fState[x][y])
  121.             {
  122.             MoveTo (hdc, x       * cxBlock, y       * cyBlock);
  123.             LineTo (hdc, (x + 1) * cxBlock, (y + 1) * cyBlock);
  124.             MoveTo (hdc, x       * cxBlock, (y + 1) * cyBlock);
  125.             LineTo (hdc, (x + 1) * cxBlock, y       * cyBlock);
  126.             }
  127.           }
  128.       EndPaint (hwnd, &ps);
  129.       return 0;
  130.        
  131.     case WM_DESTROY:
  132.       PostQuitMessage (0);
  133.       return 0;
  134.     }
  135.     
  136.   return DefWindowProc (hwnd, message, wParam, lParam);
  137.   }
  138.                   
  139.